

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class UserOperate {
	public int login(String sno,String password){
		if(search(sno)!=0){
			information info=new DatabaseOperate().infoquery("sNo",sno).get(0);
			if(password.equals(info.getPassword())){
				System.out.println("login successfully\n");
				if(info.getIsadmin()==1){
					return 2;
				}
				else{
					return 1;
				}
			}
			else {
				System.out.println("wrong password\n");
				return 0;
			}
		}
		else {
			System.out.println("this user isn't in database\n");
			return 0;
			}
	}
	public int search(String sno){
		ArrayList<information> search=new DatabaseOperate().infoquery("sNo",sno);
		return search.size();
	}
	public boolean register(String sno, String name, String password, int amount, String address, String birthday, String school, String phone, String sex, String grade, Integer age,String Likebest){
		if(search(sno)==1) {
			System.out.println("this sno is already in database!");
			return false;
		}
		if(search(sno)==0){
			try{
				new DatabaseOperate().infoinsert(new information(sno, name, password, amount, address, birthday, school, phone, sex, grade, age, Likebest));
				System.out.println("Register successfully!");
				return true;
			}catch(Exception e){
				System.out.println(e.toString());
			}
		}
		return false;
	}
	public String[] dishRecommend(String sno){
		String[] a={"Null"};
		String[] b=new String[10];
		if(search(sno)!=0){
			Dishes[] dishlist=new DishRecommend(sno).topndishes(10);
			for(int i=0;i<10;i++){
				new DatabaseOperate().recommendinsert(new Recommend(dishlist[i].getId(),dishlist[i].getCno(),dishlist[i].getDno(),sno));
			}
			for(int i=0;i<10;i++){
				b[i]=dishlist[i].getDname();
			}
			return b;
		}
		else{
			System.out.println("the user is not in the database.");
			return a;
		}	
	}
	public int riceRecommend(String sno,Date currentTime){
	    SimpleDateFormat HourFormat = new SimpleDateFormat("H");
	    SimpleDateFormat MinuteFormat = new SimpleDateFormat("m");
	    int hour=Integer.parseInt(HourFormat.format(currentTime));
	    int minute=Integer.parseInt(MinuteFormat.format(currentTime));
	    int time=hour*60+minute;
		if(search(sno)!=0){
			return new RiceRecommend("2011212095").RecommandAppetite(time);
		}
		else{
			System.out.println("the user is not in the database.");
			return 0;
		}	
	}
	public information getInfo(String sno){
		ArrayList<information> info=new DatabaseOperate().infoquery("sNo",sno);
		if(info.size()==1){
			return info.get(0);
		}
		else{
			System.out.println("the user is not in the database.");
			return null;
		}
	}

	public boolean ModifyInfo(String sno, String name, String password, int amount, String address, String birthday, String school, String phone, String sex, String grade, Integer age,String Likebest){
		if(search(sno)!=0){
			try{
				new DatabaseOperate().infoupdate(new information(sno, name, password, amount, address, birthday, school, phone, sex, grade, age, Likebest));
				System.out.println("edit successfully!");
				return true;
			}catch(Exception e){
				System.out.println(e.toString());
			}
		}
		else {
			System.out.println("this sno isn't in database!");return false;}
		return false;
	}
	public List<Window> windowList(){
	//���ص����б�
		ArrayList<Window> windlist=new DatabaseOperate().windowquery();
		return windlist;
	}
	public Window windowInfo(String cno){
		//����ĳ������Ϣ
		Window windinfo=new DatabaseOperate().windowquery("cNo",cno).get(0);
		return windinfo;
	}
	public List<Dishes> dishQueryByCno(String cno){
		//���ز˵��б�
		ArrayList<Dishes> dishlist=new DatabaseOperate().dishquery("cNo",cno);
		return dishlist;
	}
	public Dishes dishInfo(String dno){
		//���ز˵���Ϣ
		Dishes dish = new DatabaseOperate().dishquery("dNo",dno).get(0);
		return dish;
	}
	public static void main(String[] args){
		new UserOperate().dishRecommend("2011212095");
	}
}


